package services

import (
	"context"
	"database/sql"
	db "dhuviewer/db_query"
	"dhuviewer/dto"
	"time"

	"github.com/google/uuid"
	"github.com/labstack/gommon/log"
)

func GetGallery(offset int32) (error, []db.Gallery) {
	galleryData, err := queries.ViewManyGallery(context.Background(), offset)
	if err != nil {
		log.Info("error:", err)
		return err, []db.Gallery{}
	}
	return err, galleryData
}

func GetOneGallery(gid string) (error, db.Gallery) {
	galleryData, err := queries.ViewGallery(context.Background(), uuid.MustParse(gid))
	if err != nil {
		log.Info("error:", err)
		return err, db.Gallery{}
	}
	return err, galleryData
}

func GetUserGallery(userid string) (error, []db.Gallery) {
	galleryData, err := queries.ViewOnesGallery(context.Background(), uuid.MustParse(userid))
	if err != nil {
		log.Info("error:", err)
		return err, []db.Gallery{}
	}
	return err, galleryData
}

func CreateGallery(temp *dto.Gallery, userid string, username string) (error, uuid.UUID) {
	res, err := queries.CreateGallery(context.Background(), db.CreateGalleryParams{
		Images:   temp.Images,
		Userid:   uuid.MustParse(userid),
		Tags:     temp.Tags,
		Date:     sql.NullTime{Time: temp.Date, Valid: true},
		Username: sql.NullString{String: username, Valid: true},
		Token:    sql.NullString{String: temp.Token, Valid: true},
		Title:    sql.NullString{String: temp.Title, Valid: true},
	})
	if err != nil {
		log.Info("error:", err)
		return err, uuid.Nil
	}
	// fmt.Println(res)
	return nil, res.Gid
}

func DeleteGallery(gid uuid.UUID) error {
	err := queries.DeleteGallery(context.Background(), gid)
	return err
}

func DeleteOnesGallery(gid uuid.UUID, userId uuid.UUID) error {
	err := queries.DeleteOnesGallery(context.Background(), db.DeleteOnesGalleryParams{
		Gid:    gid,
		Userid: userId,
	})
	return err
}

func SelectByTag(tag string) ([]db.Gallery, error) {
	galleryData, err := queries.SelectByTags(context.Background(), tag)
	if err != nil {
		log.Info("error:", err)
		return []db.Gallery{}, err
	}
	return galleryData, err
}

func TagMVP(tag string) ([]db.CrossSocail, error) {
	galleryData, err := queries.TagMVPInfoWithCount(context.Background(), tag)
	if err != nil {
		log.Info("error:", err)
		return []db.CrossSocail{}, err
	}
	return galleryData, err
}

func TagMVPWithoutCount(tag string) ([]db.Socail, error) {
	galleryData, err := queries.TagMVPInfo(context.Background(), tag)
	if err != nil {
		log.Info("error:", err)
		return nil, err
	}
	return galleryData, err
}

func SelectByTagAndUid(tag string, uid string) ([]db.Gallery, error) {
	galleryData, err := queries.SelectByTagsAndUid(context.Background(), db.SelectByTagsAndUidParams{
		Userid:  uuid.MustParse(uid),
		Column1: tag,
	})
	if err != nil {
		log.Info("error:", err)
		return []db.Gallery{}, err
	}
	return galleryData, err
}

func SelectByTimeRange(d1 time.Time, d2 time.Time) ([]db.Gallery, error) {
	galleryData, err := queries.SelectByTimeRange(context.Background(), db.SelectByTimeRangeParams{
		Date:   sql.NullTime{Time: d1, Valid: true},
		Date_2: sql.NullTime{Time: d2, Valid: true},
	})
	if err != nil {
		log.Info("error:", err)
		return []db.Gallery{}, err
	}
	return galleryData, err
}

func StatGallery(uid uuid.UUID) ([]db.StatDate, error) {
	galleryStat, err := queries.StatGallery(context.Background(), uid)
	if err != nil {
		return nil, err
	}
	return galleryStat, nil
}
